# coding:utf-8
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt, QUrl, QPoint
from PyQt5.QtGui import QIcon, QScreen, QDesktopServices
from qfluentwidgets import NavigationItemPosition, SplitFluentWindow, FluentIcon, NavigationAvatarWidget, MessageBox, isDarkTheme, RoundMenu, Action
from resource import Icon
from ui.home.home_interface import HomeInterface
from ui.download.single_download_interface import SingleDownloadInterface
from ui.download.batch_download_interface import BatchDownloadInterface
from ui.setting.setting_interface import SettingInterface
from ui.web_engine.web import WebInterface
from config import cfg
from ui.foder.folder_interface import FolderInterface
from ui.navigarion.navigation_interface import NavigationInterface

class MainWindow(SplitFluentWindow):

    def __init__(self):
        super().__init__()
        # 创建子接口
        self.home = HomeInterface(self)
        self.singleDownload = SingleDownloadInterface(self)
        self.batchDownload = BatchDownloadInterface(self)
        self.folderInterface = FolderInterface('Folder Interface', self)
        self.settings = SettingInterface(self)
        self.web = WebInterface(self)
        self.navigation = NavigationInterface()
        # 将项目添加到导航界面
        self.initNavigation()
        self.initWindow()

    def initNavigation(self):
        self.addSubInterface(self.home, FluentIcon.HOME, '首页')
        self.navigationInterface.addSeparator()

        pos = NavigationItemPosition.SCROLL
        self.addSubInterface(self.singleDownload, FluentIcon.DOWNLOAD, '单视频下载', pos)
        self.addSubInterface(self.batchDownload, FluentIcon.ZOOM, '批量下载', pos)
        self.addSubInterface(self.folderInterface, FluentIcon.FOLDER, '下载记录', pos)
        self.addSubInterface(self.web, FluentIcon.GLOBE, '搜索引擎', pos)

        # add custom widget to bottom

        self.navigationInterface.addWidget(
            routeKey='avatar',
            widget=self.navigation,
            onClick=self.showMessageBox,
            position=NavigationItemPosition.BOTTOM,
        )
        self.addSubInterface(self.settings, FluentIcon.SETTING, '设置', NavigationItemPosition.BOTTOM)
        self.setQss()
        cfg.themeChanged.connect(self.setQss)
        self.navigationInterface.setExpandWidth(300)

    def initWindow(self):
        self.resize(1000, 700)
        self.setWindowIcon(QIcon(Icon.LOGO.path()))
        self.setWindowTitle('AI视频助手')
        self.titleBar.setAttribute(Qt.WA_StyledBackground)

        center = QScreen.availableGeometry(QApplication.primaryScreen()).center()
        geo = self.frameGeometry()
        geo.moveCenter(center)
        self.move(geo.topLeft())

    def showMessageBox(self):
        w = MessageBox(
            '支持作者🥰',
            '个人开发不易，如果这个项目帮助到了您，可以考虑请作者喝一瓶快乐水🥤。您的支持就是作者开发和维护项目的动力🚀',
            self
        )
        w.yesButton.setText('打开抖音')
        w.cancelButton.setText('关闭')

        if w.exec():
            QDesktopServices.openUrl(QUrl("https://www.douyin.com/"))

    def setQss(self):
        theme = 'dark' if isDarkTheme() else 'light'
        with open(f'resource/qss/{theme}/demo.qss', encoding='utf-8') as f:
            self.setStyleSheet(f.read())
